package com.randev.snapdb;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AlbumDetails {
	
	private final String TAG = "AlbumDetails";
	private static final String KEY_ROW_ID = "row_id";
	private static final String KEY_ALBUM_ID = "album_id";
	private static final String KEY_ALBUM_TITLE = "album_title";
	private static final String KEY_ALBUM_DESCRIPTION = "album_description";
	private static final String KEY_GEO_LONGITUDE = "geo_longitude";
	private static final String KEY_GEO_LATITUDE = "geo_latitude";
	private static final String KEY_LOCATION = "location";
    
	private String[] columns = new String[] {KEY_ROW_ID, KEY_ALBUM_ID, 
			KEY_ALBUM_TITLE, KEY_ALBUM_DESCRIPTION, KEY_GEO_LONGITUDE, KEY_GEO_LATITUDE, KEY_LOCATION};
	
	
	private static final String TABLE_NAME = "album_details";
    public final Context albumDetailsContext;
    public DatabaseHelper databaseHelper;
    private SQLiteDatabase database;
	
	
	private static final String ALBUM_DETAILS_CREATE = "CREATE TABLE " + TABLE_NAME
			+ " (" + KEY_ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ KEY_ALBUM_ID + " TEXT ," 
			+ KEY_ALBUM_TITLE + " TEXT ,"
			+ KEY_ALBUM_DESCRIPTION + " TEXT ,"
			+ KEY_GEO_LONGITUDE + " TEXT ,"
			+ KEY_GEO_LATITUDE + " TEXT ,"
			+ KEY_LOCATION +  " TEXT);";
	


	
	
    public static void onCreate(SQLiteDatabase database) throws SQLException {
            database.execSQL(ALBUM_DETAILS_CREATE);
    }

    public static void onUpgrade(SQLiteDatabase database, int oldVersion,
                    int newVersion) throws SQLException {
            database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(database);
    }

    public AlbumDetails(Context c) {
    	albumDetailsContext = c;
    }

    public AlbumDetails openWritableDatabase() throws SQLException {
            databaseHelper = new DatabaseHelper(albumDetailsContext);
            database = databaseHelper.getWritableDatabase();
            return this;

    }

    public AlbumDetails openReadableDatabase() throws SQLException {
            databaseHelper = new DatabaseHelper(albumDetailsContext);
            database = databaseHelper.getReadableDatabase();
            return this;

    }

    public void closeDatabase() throws SQLException {
            databaseHelper.close();
    }
    
    public long insertAlbumDetails(String albumId, String albumTitle, String albumDescription, 
    		String longitude, String latitude, String location) throws SQLException {
    	
    	ContentValues cv = new ContentValues();
    	cv.put(KEY_ALBUM_ID, albumId);
    	cv.put(KEY_ALBUM_TITLE, albumTitle);
    	cv.put(KEY_ALBUM_DESCRIPTION, albumDescription);
    	cv.put(KEY_GEO_LONGITUDE, longitude);
    	cv.put(KEY_GEO_LATITUDE, latitude);
    	cv.put(KEY_LOCATION, location);
    	
    	Log.w(TAG, "album id" + albumId);
    	
    	return database.insert(TABLE_NAME, null, cv);
    }
    
    public ArrayList<String[]> getAllAlbumDetails() throws SQLException {
    	ArrayList<String[]> allAlbumDetails = new ArrayList<String[]>();
    	
    	Cursor cursor = database.query(TABLE_NAME, columns, null, null, null, null, null);
    	
    	if (cursor.getCount() != 0) {
    		cursor.moveToFirst();
        	
        	while (!cursor.isAfterLast()) {
        		String[] albumDetails = new String[cursor.getColumnCount()];
        		albumDetails[0] = cursor.getString(0);
        		albumDetails[1] = cursor.getString(1);
        		albumDetails[2] = cursor.getString(2);
        		albumDetails[3] = cursor.getString(3);
        		albumDetails[4] = cursor.getString(4);
        		albumDetails[5] = cursor.getString(5);
        		albumDetails[6] = cursor.getString(6);
        		
        		allAlbumDetails.add(albumDetails);
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(0));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(1));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(2));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(3));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(4));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(5));
        		Log.w(TAG, "getAllAlbumDetails: " + cursor.getString(6));
        		cursor.moveToNext();
        	}
    	}
    	return allAlbumDetails;
    }
    
    public ArrayList<String> getAlbumDetailsByAlbumId(String title) throws SQLException {
    	ArrayList<String> albumDetails = new ArrayList<String>();
    	
    	Cursor cursor = database.query(TABLE_NAME, columns, KEY_ALBUM_TITLE+"=?", new String[] {title}, null, null, null);
    	
    	if (cursor.getCount() != 0) {
    		cursor.moveToFirst();
    		
    		albumDetails.add(cursor.getString(0));
    		albumDetails.add(cursor.getString(1));
    		albumDetails.add(cursor.getString(2));
    		albumDetails.add(cursor.getString(3));
    		albumDetails.add(cursor.getString(4));
    		albumDetails.add(cursor.getString(5));
    		albumDetails.add(cursor.getString(6));
    	}
    	return albumDetails;
    }
}
